package com.zbkj.crmeb.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.store.dao.StoreProductGroupActivityDao;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductGroupActivity;
import com.zbkj.crmeb.store.service.StoreProductGroupActivityService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StoreProductGroupActivityServiceImpl extends ServiceImpl<StoreProductGroupActivityDao, StoreProductGroupActivity> implements StoreProductGroupActivityService {

    @Autowired
    private StoreProductGroupActivityDao dao;

    /**
     * 功能描述: 通过活动id获取商品id
     * @Param: [id]
     * @Return: java.lang.Integer
     * @Author: wangzh
     * @Date: 2022/11/29 14:50
     */
    @Override
    public Integer getProductIdByActivityId(Integer id) {
        StoreProductGroupActivity one = this.getOne(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .eq(StoreProductGroupActivity::getActivityId, id)
                .groupBy(StoreProductGroupActivity::getProductId)
                .last("limit 1"));
        return one.getProductId();
    }

    /**
     * 功能描述: 通过活动id获取商品id
     * @Param: [id]
     * @Return: java.lang.Integer
     * @Author: wangzh
     * @Date: 2022/11/29 14:50
     */
    @Override
    public Integer getProductIdByActivityId2(Integer id) {
        StoreProductGroupActivity one = this.getOne(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .eq(StoreProductGroupActivity::getActivityId, id)
                .groupBy(StoreProductGroupActivity::getProductId)
                .last("limit 1"));
        return one.getProductId();
    }

    /**
     * 功能描述: 通过活动id获取商品id
     * @Param: [id]
     * @Return: java.lang.Integer
     * @Author: wangzh
     * @Date: 2022/11/29 14:50
     */
    @Override
    public List<Integer> getActivityIdsByActivityId(Integer id) {
        StoreProductGroupActivity one = this.getOne(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .eq(StoreProductGroupActivity::getActivityId, id)
                .last("limit 1"));
        String groupId = one.getGroupId();
        List<StoreProductGroupActivity> list = this.list(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .eq(StoreProductGroupActivity::getGroupId, groupId));
        return list.stream().map(StoreProductGroupActivity::getActivityId).distinct().collect(Collectors.toList());

    }

    /**
     * 功能描述: 通过活动唯一id获取活动id
     * @Param: [id]
     * @Return: com.zbkj.crmeb.store.model.StoreProductGroupActivity
     * @Author: wangzh
     * @Date: 2022/11/29 14:18
     */
    public List<Integer> getActivityIdsByGroupId(String id){
        List<StoreProductGroupActivity> list = this.list(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .eq(StoreProductGroupActivity::getGroupId, id));
        return list.stream().map(StoreProductGroupActivity::getActivityId).distinct().collect(Collectors.toList());

    }

    /**
     * 功能描述: 通过活动id获取商品id
     * @Param: [id]
     * @Return: java.lang.Integer
     * @Author: wangzh
     * @Date: 2022/11/29 14:50
     */
    @Override
    public String getGroupIdByActivityId(Integer id) {
        StoreProductGroupActivity one = this.getOne(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .eq(StoreProductGroupActivity::getActivityId, id)
                .last("limit 1"));
        return one.getGroupId();
    }

    /**
     * 功能描述: 通过商品id移除商品拼团信息
     * @Param: [productId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2022/11/29 14:48
     */
    @Override
    public Boolean removeProductByProductIdAndGroupId(Integer productId, String groupId) {
        if(StringUtils.isNotEmpty(groupId)) {
            return this.remove(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getProductId, productId)
                    .eq(StoreProductGroupActivity::getGroupId, groupId)
            );
        }else {
            return this.remove(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getProductId, productId)
                    .isNull(StoreProductGroupActivity::getGroupId)
            );
        }
    }

    @Override
    public Boolean getProductInfoIsExistByProductId(Integer productId, List<Integer> activityIds) {
        int count = 0;
        if (CollectionUtils.isNotEmpty(activityIds)){
            count = this.count(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getProductId, productId)
                    .in(StoreProductGroupActivity::getActivityId, activityIds));
        }

        return count > 0;
    }

    /**
     * 功能描述: 通过活动id获取商品信息
     * @Param: [activityIds]
     * @Return: java.util.List<com.zbkj.crmeb.store.model.StoreProductGroupActivity>
     * @Author: wangzh
     * @Date: 2022/11/29 17:11
     */
    @Override
    public List<StoreProductGroupActivity> getByActivityIds(List<Integer> activityIds) {
        if (CollectionUtils.isEmpty(activityIds)){
            return new ArrayList<>();
        }
        return this.list(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .in(StoreProductGroupActivity::getActivityId, activityIds)
                .groupBy(StoreProductGroupActivity::getActivityId));
    }

    /**
     * 功能描述: 通过商品id获取拼团商品信息
     * @Param: [productId]
     * @Return: java.util.List<com.zbkj.crmeb.store.model.StoreProductGroupActivity>
     * @Author: wangzh
     * @Date: 2022/11/30 10:57
     */
    @Override
    public List<StoreProductGroupActivity> getProductInfoByproductId(Integer productId, List<Integer> activityIds, String groupId) {
        log.info("getProductInfoByproductId：{},{},{}", productId, JSONObject.toJSONString(activityIds), groupId);
        if(StringUtils.isNotEmpty(groupId) && !"0".equals(groupId)){
            List<StoreProductGroupActivity> activityList = this.list(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getProductId, productId)
                    .eq(StoreProductGroupActivity::getGroupId, groupId));
            log.info("getProductInfoByproductId activityList：{}", JSONObject.toJSONString(activityList));
            return activityList;
        }
//        List<StoreProductGroupActivity> activityList2 =  this.list(Wrappers.<StoreProductGroupActivity>lambdaQuery()
//                .eq(StoreProductGroupActivity::getProductId, productId)
//                .in(CollectionUtils.isNotEmpty(activityIds), StoreProductGroupActivity::getActivityId, activityIds)
////                .isNull(StoreProductGroupActivity::getGroupId)
//        );
        log.info("getProductInfoByproductId activityList2：nullllll");
        return null;
    }

    /**
     * 功能描述: 通过活动id查找子表，通过商品id分组，返回是否是阶梯拼团活动，用于设置拼团活动参团人数
     * @Param: [activity]
     * @Return: java.util.List<java.lang.Integer>
     * @Author: wangzh
     * @Date: 2022/11/30 14:36
     */
    @Override
    public List<Integer> getActivityIdsByActivityId(Integer activity, List<Integer> ids, String groupId) {
        if (CollectionUtils.isEmpty(ids)){
            return new ArrayList<>(activity);
        }
        List<StoreProductGroupActivity> list = new ArrayList<>();
        if(StringUtils.isNotBlank(groupId)){
            list = this.list(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getProductId, getProductIdByActivityId2(activity))
                    .eq(StoreProductGroupActivity::getGroupId, groupId));
        }else {
            list = this.list(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getProductId, getProductIdByActivityId2(activity))
                    .in(StoreProductGroupActivity::getActivityId, ids)
                    .isNull(StoreProductGroupActivity::getGroupId));
        }
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>(activity);
        }
        // 这里是一个商品对应多个 StoreProductGroupActivity 就是阶梯拼团了
        return list.stream().map(StoreProductGroupActivity::getActivityId).distinct().collect(Collectors.toList());
    }

    @Override
    public Boolean removeProductByActivityId(List<Integer> ids) {
        return this.remove(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .in(StoreProductGroupActivity::getActivityId, ids));
    }

    @Override
    public List<StoreProductGroupActivity> getActivityId(List<Integer> ids, List<Integer> productIdList) {
        return dao.getActivityId(ids, productIdList);
    }

    /**
     * 功能描述: 通过活动id查找子表，通过商品id分组，返回是否是阶梯拼团活动，用于设置拼团活动参团人数
     * @Param: [activity]
     * @Return: java.util.List<java.lang.Integer>
     * @Author: wangzh
     * @Date: 2022/11/30 14:36
     */
    @Override
    public StoreProductGroupActivity getByIdAndActivityId(Integer skuId, String groupId) {
        log.info("getByIdAndActivityId 入口：{},{}", skuId, groupId);
        if (null == skuId && StringUtils.isEmpty(groupId)){
            return null;
        }
        StoreProductGroupActivity groupActivity ;
        if(StringUtils.isNotEmpty(groupId)){
            groupActivity = this.getOne(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getGroupId, groupId)
                    .eq(StoreProductGroupActivity::getId, skuId)
                    .last("limit 1"));
            log.info("getByIdAndActivityId groupActivity1：{}", JSONObject.toJSONString(groupActivity));

        }else {
            groupActivity = this.getOne(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                    .eq(StoreProductGroupActivity::getId, skuId)
                    .isNull(StoreProductGroupActivity::getGroupId)
                    .last("limit 1"));
            log.info("getByIdAndActivityId groupActivity2：{}", JSONObject.toJSONString(groupActivity));

        }
        return groupActivity;
    }

    /**
     * 功能描述: 通过活动id查找子表，通过商品id分组，返回是否是阶梯拼团活动，用于设置拼团活动参团人数
     * @Param: [activity]
     * @Return: java.util.List<java.lang.Integer>
     * @Author: wangzh
     * @Date: 2022/11/30 14:36
     */
    @Override
    public StoreProductGroupActivity getByIdAndActivityIdList(List<Integer> groupActivityIds, Integer productId) {
        log.info("getByIdAndActivityIdList 入口：{},{}", JSONObject.toJSONString(groupActivityIds), productId);
        if (CollectionUtils.isEmpty(groupActivityIds) ||  null == productId || 0 == productId){
            return null;
        }
        StoreProductGroupActivity groupActivity = this.getOne(Wrappers.<StoreProductGroupActivity>lambdaQuery()
                .in(StoreProductGroupActivity::getActivityId, groupActivityIds)
                .eq(StoreProductGroupActivity::getProductId, productId)
                .orderByDesc(StoreProductGroupActivity::getCreateTime)
                .last("limit 1"));

        return groupActivity;
    }


}
